package com.cqupt.controller;

import com.cqupt.common.log.annotation.OperationLog;
import com.cqupt.pojo.ResponseMessage;
import com.cqupt.pojo.User;
import com.cqupt.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 */
@CrossOrigin
@Controller
@RequestMapping("user")
@ResponseBody
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param user
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/login")
    public ResponseMessage login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> loginResult = userService.login(user);
        HttpSession session = request.getSession();
        ResponseMessage responseMessage = new ResponseMessage();
        /**
         * 查询结果放入session
         */
        if (loginResult != null) {
            //
            /**
             * 查询用户日志开关状态
             *
             */
            String log_status = userService.getUserLogStatus((String) loginResult.get("user_id"));
            loginResult.put("logSwitch", log_status);
            /**
             * 给session里面放入值
             */
            session.setAttribute("loginResult", loginResult);
            session.setAttribute("username",(String) loginResult.get("username"));
            session.setAttribute("user_id",(String) loginResult.get("user_id"));
            session.setAttribute("logSwitch", log_status);
            responseMessage.setContent(new HashMap<String, Object>(){{
                put("loginResult", loginResult);
            }});
            responseMessage.setStatus(true);
        } else {
            responseMessage.setStatus(false);
        }
        return responseMessage;
    }

    /**
     * 注销
     * @param params
     * @return
     */
    @RequestMapping("/logout")
    public ResponseMessage logout(@RequestBody Map<String, Object> params) {
        ResponseMessage responseMessage = new ResponseMessage();
        /**
         * 调用注销函数
         */
        Boolean status = userService.logout(params.get("username").toString());
        responseMessage.setStatus(status);
        return responseMessage;
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @OperationLog(level = "信息", object = "用户", operationType = "新增")
    @RequestMapping("/add_user")
    public ResponseMessage addUser(@RequestBody User user) {
        Boolean status = userService.addUser(user);
        String name = user.getUsername();
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("log_objectName",name);
        /**
         * 判断状态返回对应的值
         */
        if (status == true){
            resMap.put("log_description","");
        }else{
            responseMessage.setErrorCode("");
        }
        responseMessage.setStatus(status);
        responseMessage.setContent(resMap);
        return responseMessage;
    }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @OperationLog(level = "信息", object = "用户", operationType = "删除")
    @RequestMapping("/delete_user")
    public ResponseMessage deleteUser(@RequestBody User user) {
        Boolean status = userService.deleteUser(user);
        String name = user.getUsername();
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("log_objectName",name);
        /**
         * 根据用户状态判断，返回对应的值
         */
        if (status == true){
            resMap.put("log_description","");
        }else{
            responseMessage.setErrorCode("");
        }
        responseMessage.setStatus(status);
        responseMessage.setContent(resMap);
        return responseMessage;
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @OperationLog(level = "信息", object = "用户", operationType = "修改")
    @RequestMapping("/change_user")
    public ResponseMessage changeUser(@RequestBody User user) {
        Boolean status = userService.changeUser(user);
        String name = user.getUsername();
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("log_objectName",name);
        /**
         * 根据用户状态判断，返回对应的值
         */
        if (status == true){
            resMap.put("log_description","");
        }else{
            responseMessage.setErrorCode("");
        }
        responseMessage.setStatus(status);
        responseMessage.setContent(resMap);
        return responseMessage;
    }

    /**
     * 通过分页获取用户列表
     * @param param
     * @return
     */
    @RequestMapping("/get_user_by_page_number")
    public ResponseMessage getUserByPageNumber(@RequestBody Map<String,Object> param) {
        /**
         * 取页码
         */
        int pageNumber = Integer.parseInt(param.get("pageNumber").toString());
        /**
         * 取每页大小
         */
        int pageSize = Integer.parseInt(param.get("pageSize").toString());
        PageInfo<User> pageInfo = userService.getUserByPageNumber(pageNumber, pageSize);
        ResponseMessage responseMessage = new ResponseMessage();
        /**
         * 返回对象封装值
         */
        responseMessage.setStatus(true);
        responseMessage.setContent(new HashMap<String, Object>(){{
            put("pageInfo", pageInfo);
        }});
        return responseMessage;
    }

    /**
     * 查找用户
     * @param param
     * @return
     */
    @RequestMapping("/search_user")
    public ResponseMessage searchUser(@RequestBody Map<String,Object> param) {
        /**
         * 取页码
         */

        int pageNumber = Integer.parseInt(param.get("pageNumber").toString());
        /**
         * 取每页大小
         */
        int pageSize = Integer.parseInt(param.get("pageSize").toString());
        User user = (User)param.get("user");
        PageInfo<User> pageInfo = userService.searchUser(pageNumber, pageSize, user);
        /**
         * 封装返回对象
         */
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(new HashMap<String, Object>(){{
            put("pageInfo", pageInfo);
        }});
        return responseMessage;
    }

    /**
     * 通过用户id获取用户
     * @param param
     * @return
     */
    @RequestMapping("/get_user_by_user_id")
    public ResponseMessage getUserByUserId(@RequestBody Map<String,Object> param) {
        /**
         * 获取用户id
         */
        String userId = param.get("userId").toString();
        User result = userService.getUserByUserId(userId);
        /**
         * 封装返回对象
         */
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(new HashMap<String, Object>(){{
            put("user", result);
        }});
        return responseMessage;
    }
}
